//
//  AIFeedbackController.m
//  Mia
//
//  Created by Vill on 2024/1/15.
//

#import "AIFeedbackController.h"
#import "UITextView+Placeholder.h"
#import "AIFeedModel.h"

@interface AIFeedbackController ()<UITextFieldDelegate,UITextViewDelegate>

@property (nonatomic, strong) UIButton *backBtn;
@property (nonatomic, strong) UIButton *doneBtn;
@property (nonatomic, strong) UILabel *feedTitle;

// email
@property (nonatomic, strong) UIView *emailBg;
@property (nonatomic, strong) UITextField *emailTxt;

// 图片
@property (nonatomic, strong) UIImageView *iconImg;
@property (nonatomic, strong) UIButton *deleteBtn;

// Please fill in the feedback
@property (nonatomic, strong) UIView *contentBg;
@property (nonatomic, strong) UITextView *contentView;

@property (nonatomic, copy) NSString *chooseImgUrl;
@property (nonatomic, strong) AIFeedModel *feedModel;


@end

@implementation AIFeedbackController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
}

- (void)viewWillDisappear:(BOOL)animated {
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    [super viewWillDisappear:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorHex:@"#F5F5F5"];
    
    [self.view addSubview:self.backBtn];
    [self.view addSubview:self.feedTitle];
    [self.view addSubview:self.doneBtn];
    [self.view addSubview:self.emailBg];
    [self.emailBg addSubview:self.emailTxt];
    [self.view addSubview:self.iconImg];
    [self.view addSubview:self.deleteBtn];
    
    [self.view addSubview:self.contentBg];
    [self.contentBg addSubview:self.contentView];
    
    [self.backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(PTW(44));
        make.leading.mas_equalTo(self.view).mas_offset(PTW(10));
        make.top.mas_equalTo(self.view).mas_offset(KStatusBarHeight);
    }];
    [self.feedTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.centerY.mas_equalTo(self.backBtn);
    }];
    [self.doneBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(PTW(50), 44));
        make.trailing.mas_equalTo(self.view).mas_offset(-PTW(10));
        make.centerY.mas_equalTo(self.backBtn);
    }];
    
    [self.emailBg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.view).mas_offset(PTW(15));
        make.trailing.mas_equalTo(self.view).mas_offset(-PTW(15));
        make.height.mas_equalTo(PTW(55));
        make.top.mas_equalTo(self.view).mas_offset(PTW(25) + KNavigationBarHeight);
    }];
    [self.emailTxt mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.emailBg).mas_offset(PTW(25));
        make.top.bottom.mas_equalTo(self.emailBg);
        make.trailing.mas_equalTo(self.emailBg).mas_offset(-PTW(20));
    }];
    [self.iconImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(PTW(180));
        make.leading.mas_equalTo(self.view).mas_offset(PTW(15));
        make.top.mas_equalTo(self.emailBg.mas_bottom).mas_offset(PTW(20));
    }];
    [self.deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(PTW(20));
        make.top.trailing.mas_equalTo(self.iconImg);
    }];
    
    [self.contentBg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.view).mas_offset(PTW(15));
        make.trailing.mas_equalTo(self.view).mas_offset(-PTW(15));
        make.height.mas_equalTo(PTW(200));
        make.top.mas_equalTo(self.iconImg.mas_bottom).mas_offset(PTW(25));
    }];
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.contentBg).mas_offset(PTW(20));
        make.top.mas_equalTo(self.contentBg).mas_offset(PTW(10));
        make.trailing.mas_equalTo(self.contentBg).mas_offset(-PTW(20));
        make.bottom.mas_equalTo(self.contentBg).mas_offset(-PTW(10));
    }];
}

// 确认
-(void)doneAction{
    if (![self.emailTxt.text strHasValue]){
        [SVProgressHUD showErrorWithStatus:@"Please input your email."];
        [SVProgressHUD dismissWithDelay:1];
        return;
    }
    if (![NSString emailEvalunte:self.emailTxt.text]){
        [SVProgressHUD showErrorWithStatus:@"Please enter a valid email address."];
        [SVProgressHUD dismissWithDelay:1];
        return;
    }
    if (![self.contentView.text strHasValue]){
        [SVProgressHUD showErrorWithStatus:@"Please enter your feedback."];
        [SVProgressHUD dismissWithDelay:1];
        return;
    }
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"content"] = self.contentView.text;
    param[@"email"] = self.emailTxt.text;
    if ([self.feedModel.url strHasValue]){
        param[@"img"] = self.feedModel.url;
    }
    [AIManager requestMethod:@"POST" url:api_feedbackAdd parameters:param successBlock:^(id  _Nullable result) {
        [SVProgressHUD showSuccessWithStatus:@"Thank you for your submission!"];
        [SVProgressHUD dismissWithDelay:2];
         dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(),^{//主线程
             [self.navigationController popViewControllerAnimated:YES];
         });
    } failedBlock:^(NSError * _Nullable error) {
        [SVProgressHUD showErrorWithStatus:@"Please try again later!"];
        [SVProgressHUD dismissWithDelay:2];
    } showHUD:YES];
}

// 删除
-(void)deleteAction{
    self.iconImg.image = KImaged(@"ai_feedback_img");
    self.deleteBtn.hidden = YES;
}

-(void)uploadImg{
    WeakSelf(self);
    [self chooseRictureWithResult:^(UIImage * _Nonnull chooseImage) {
        [weakself uploadAvatar:chooseImage];
    }];
}

// 选择照片
- (void)uploadAvatar:(UIImage *)chooseImg{
    [AIManager uploadImage:chooseImg url:api_uploadFiles successBlock:^(id  _Nullable result) {
        NSMutableArray *tempArr = result;
        if (tempArr.count > 0){
            self.feedModel = [AIFeedModel mj_objectWithKeyValues:tempArr.firstObject];
            NSLog(@" ------ %@",self.feedModel.url);
            self.iconImg.image = chooseImg;
            self.deleteBtn.hidden = NO;
        }
    } failedBlock:^(NSError * _Nullable error) {
        NSLog(@"%@",error);
    } showHUD:YES];
}


#pragma mark -------------- lazy -----------
- (UIButton *)backBtn{
    if(!_backBtn){
        _backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_backBtn setImage:KImaged(@"ai_setting_back") forState:UIControlStateNormal];
        [_backBtn addTarget:self action:@selector(fanhuiAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _backBtn;
}

- (void)fanhuiAction{
    [self.navigationController popViewControllerAnimated:YES];
}

- (UILabel *)feedTitle{
    if (!_feedTitle){
        _feedTitle = [UILabel new];
        _feedTitle.text = @"Feedback";
        _feedTitle.font = [UIFont fontWithWeight:Medium size:PTW(17)];
        _feedTitle.textColor = [UIColor colorHex:@"#333333"];
    }
    return _feedTitle;
}

- (UIButton *)doneBtn{
    if(!_doneBtn){
        _doneBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_doneBtn setTitle:@"Done" forState:UIControlStateNormal];
        [_doneBtn setTitleColor:[UIColor colorHex:@"#333333"] forState:UIControlStateNormal];
        _doneBtn.titleLabel.font = [UIFont fontWithWeight:Regular size:PTW(15)];
        [_doneBtn addTarget:self action:@selector(doneAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _doneBtn;
}

-(UIView *)emailBg{
    if(!_emailBg){
        _emailBg = [UIView new];
        _emailBg.layer.cornerRadius = PTW(10);
        _emailBg.layer.masksToBounds = YES;
        _emailBg.backgroundColor = UIColor.whiteColor;
    }
    return _emailBg;
}

- (UITextField *)emailTxt{
    if(!_emailTxt){
        _emailTxt = [UITextField new];
        _emailTxt.placeholder = @"Your Email";
        _emailTxt.placeholderColor = [UIColor colorHex:@"#333333" alpha:0.5];
        _emailTxt.textColor = [UIColor colorHex:@"#333333"];
        [_emailTxt setModifyClearButton:@"ai_textfield_close"];
        _emailTxt.delegate = self;
    }
    return _emailTxt;
}


- (UIImageView *)iconImg{
    if(!_iconImg){
        _iconImg = [UIImageView new];
        _iconImg.image = KImaged(@"ai_feedback_img");
        _iconImg.contentMode = UIViewContentModeScaleAspectFill;
        _iconImg.layer.cornerRadius = PTW(10);
        _iconImg.layer.masksToBounds = YES;
        WeakSelf(self);
        [_iconImg addTapGestureWithBlock:^(UIGestureRecognizer * _Nonnull gesture) {
            [weakself uploadImg];
        }];
    }
    return _iconImg;
}

- (UIButton *)deleteBtn{
    if(!_deleteBtn){
        _deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _deleteBtn.hidden = YES;
        [_deleteBtn setImage:KImaged(@"ai_feedback_delete") forState:UIControlStateNormal];
        [_deleteBtn addTarget:self action:@selector(deleteAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _deleteBtn;
}

// 代理方法
 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (textField.text.length - range.length + string.length > 30){
        return NO;
    }
    return YES;
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    NSString *content = [textView.text stringByReplacingCharactersInRange:range withString:text];
    if (content.length > 200) {
        return NO;
    }
    return YES;
}

-(UIView *)contentBg{
    if(!_contentBg){
        _contentBg = [UIView new];
        _contentBg.layer.cornerRadius = PTW(10);
        _contentBg.layer.masksToBounds = YES;
        _contentBg.backgroundColor = UIColor.whiteColor;
    }
    return _contentBg;
}

-(UITextView *)contentView{
    if(!_contentView){
        _contentView = [[UITextView alloc] init];
        _contentView.placeholder = @"Please fill in the feedback(200 limited)";
        _contentView.placeholderColor = [UIColor colorHex:@"#333333" alpha:0.5];
        _contentView.textColor = [UIColor colorHex:@"#333333"];
        _contentView.font = [UIFont fontWithWeight:Regular size:PTW(15)];
        _contentView.delegate = self;
    }
    return _contentView;
}


@end
